package com.imooc.controller;

import com.drore.www.logbeansecret.bean.CommitBean;
import com.github.pagehelper.PageInfo;
import com.imooc.pojo.IMoocJSONResult;
import com.imooc.pojo.Logan;
import com.imooc.service.LoganService;
import com.imooc.utils.RequestUtil;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/logan")
public class LoganController {

    @Autowired
    private LoganService loganService;

    @Autowired
    private Sid sid;

    //http://172.16.10.236:8080/logan/getLoganListUrl?
    //{"page":"10","pageSize":"20"}
    @PostMapping("/getLoganList")
    @ResponseBody
    public IMoocJSONResult queryLoganList(HttpServletRequest request) {
        int[] pagebean = RequestUtil.getHeadParamesInt(request);
        int startPage = pagebean[0];
        int pageSize = pagebean[1];
        PageInfo<Logan> loganList = loganService.queryLoganList(startPage, pageSize);
        return IMoocJSONResult.ok(loganList);
    }

    //http://172.16.10.236:8080/logan/getLoganListUrl?page=1&pageSize=200
    @PostMapping("/getLoganListUrl")
    @ResponseBody
    public IMoocJSONResult queryLoganListUrl(HttpServletRequest request) {
//        Enumeration headerNames = request.getHeaderNames();
//        while (headerNames.hasMoreElements()) {
//            String key = (String) headerNames.nextElement();
//            String value = request.getHeader(key);
//            System.out.println(key + "的值为:" + value);
//        }
        int startPage = RequestUtil.getParamesInt(request, "page", 1);
        int pageSize = RequestUtil.getParamesInt(request, "pageSize", 5);
        PageInfo<Logan> loganList = loganService.queryLoganList(startPage, pageSize);
        return IMoocJSONResult.ok(loganList);
    }

    @PostMapping("/uploadGPS")
    @ResponseBody
    public IMoocJSONResult uploadGPS(HttpServletRequest request) {
        String list = RequestUtil.getHeadParames(request);
        System.out.println(System.currentTimeMillis() + "uploadGPS: " + list);
        return IMoocJSONResult.ok();
    }

    @PostMapping("/insertLoganList")
    @ResponseBody
    public IMoocJSONResult insertLoganList(HttpServletRequest request) {
        CommitBean list = RequestUtil.getHeadParames(request, new CommitBean());
        System.out.println("logan/insertLoganList: \n" + "手机信息数量：" + list.getPhoneBeanList().size()
                + "\n事件信息数量：" + list.getEventBeanList().size()
                + "\n请求信息数量：" + list.getNetConnectBeanList().size());
//        loganService.insertIntoLogan(list);
        return IMoocJSONResult.ok();
    }

    @PostMapping("/uploadSomething")
    @ResponseBody
    public IMoocJSONResult uploadSomething(HttpServletRequest request) {
        System.err.println("uploadSomething 。。。");
        return IMoocJSONResult.ok();
    }
}
